{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(2.3026)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "criterion = torch.nn.BCELoss()\n",
    "\n",
    "criterion(torch.tensor(0.1), torch.tensor(1.0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Hparams(dict):\n",
    "    def __getattr__(self, attr):\n",
    "        return self[attr]\n",
    "\n",
    "    def __setattr__(self, attr, value):\n",
    "        self[attr] = value\n",
    "\n",
    "\n",
    "mydict = Hparams(a=3)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mydict.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "__init__() got an unexpected keyword argument 'k'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[51], line 22\u001b[0m\n\u001b[1;32m     16\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, key)\n\u001b[1;32m     17\u001b[0m     \u001b[38;5;66;03m# def __iter__(self):\u001b[39;00m\n\u001b[1;32m     18\u001b[0m     \u001b[38;5;66;03m#     # Return an iterator over key-value pairs\u001b[39;00m\n\u001b[1;32m     19\u001b[0m     \u001b[38;5;66;03m#     for key in self.__dict__:\u001b[39;00m\n\u001b[1;32m     20\u001b[0m     \u001b[38;5;66;03m#         yield key, getattr(self, key)\u001b[39;00m\n\u001b[0;32m---> 22\u001b[0m hparms \u001b[38;5;241m=\u001b[39m \u001b[43mHparms\u001b[49m\u001b[43m(\u001b[49m\u001b[43mk\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mdf\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m     25\u001b[0m \u001b[38;5;28mprint\u001b[39m({\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhparms})\n",
      "\u001b[0;31mTypeError\u001b[0m: __init__() got an unexpected keyword argument 'k'"
     ]
    }
   ],
   "source": [
    "import dotwiz\n",
    "from typing import TYPE_CHECKING\n",
    "\n",
    "\n",
    "# To avoid creating a class at runtime, for type-hinting alone.\n",
    "from dataclasses import dataclass\n",
    "\n",
    "# Map the `dict` fields here\n",
    "# @dataclass\n",
    "class Hparms(dict):\n",
    "    name: str = 'alen'\n",
    "    price: float = 19\n",
    "    \n",
    "    \n",
    "    def __getitem__(self, key):\n",
    "        return getattr(self, key)\n",
    "    # def __iter__(self):\n",
    "    #     # Return an iterator over key-value pairs\n",
    "    #     for key in self.__dict__:\n",
    "    #         yield key, getattr(self, key)\n",
    "            \n",
    "hparms = Hparms(k=5, name='df')\n",
    "\n",
    "\n",
    "print({**hparms})\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'b': 2}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class cumdict(dict):\n",
    "    pass\n",
    "\n",
    "mydi = cumdict(a=1, b=2)\n",
    "mydi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "expression cannot contain assignment, perhaps you meant \"==\"? (3135671537.py, line 14)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[43], line 14\u001b[0;36m\u001b[0m\n\u001b[0;31m    hparms = Hparms('k' = 5)\u001b[0m\n\u001b[0m                    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m expression cannot contain assignment, perhaps you meant \"==\"?\n"
     ]
    }
   ],
   "source": [
    "class Hparms(dict):\n",
    "    name: str = 'alen'\n",
    "    price: float = 19\n",
    "    def __init__(self, **kwargs):\n",
    "        super().__init__(**kwargs)\n",
    "    \n",
    "    def __getitem__(self, key):\n",
    "        return getattr(self, key)\n",
    "    def __iter__(self):\n",
    "        # Return an iterator over key-value pairs\n",
    "        for key in self.__dict__:\n",
    "            yield key, getattr(self, key)\n",
    "            \n",
    "hparms = Hparms('k' = 5)\n",
    "\n",
    "\n",
    "print({**hparms})\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'batch_size': 32, 'epochs': 2000}\n"
     ]
    }
   ],
   "source": [
    "class Hparams(dict):\n",
    "    def __getattr__(self, attr):\n",
    "        return self[attr]\n",
    "\n",
    "    def __setattr__(self, attr, value):\n",
    "        self[attr] = value\n",
    "    \n",
    "# hparams = Hparams(\n",
    "#     epochs = 100,\n",
    "#     lr = 1e-3,\n",
    "#     batch_size = 32,\n",
    "# )\n",
    "\n",
    "hparams = Hparams(\n",
    "    batch_size = 32, \n",
    "    epochs = 2000,\n",
    ")\n",
    "\n",
    "print({**hparams})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hparms.price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'alen'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hparms.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'abs': 3, 'b': 4}\n"
     ]
    }
   ],
   "source": [
    "mydict = {'abs': 3, 'b': 4}\n",
    "print({**mydict})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dl4",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
